#include "searchwindow.h"

#include <QtDebug>

// configure from designer form
SearchWindow::SearchWindow()
{
    // create designer form
    setupUi(this);
}

// wire up widgets
void SearchWindow::startConnections()
{
    //connect signals and slots
    connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(typingFinished()));
    connect(searchButton, SIGNAL(clicked()), this, SLOT(typingFinished()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(hide()));
    connect(lineEdit, SIGNAL(textChanged(QString)), this, SLOT(typingFinished()));
}

/* SearchAndShow Method
 * This method is used to show the search window. It takes in a QString
 * called text which it sets as the text in the lineEdit of the search window.
 * Depending on the QString given, it then calls the appropriate method display
 * the search results. FillListWidget method is called if a blank string has been passed,
 * while the updateListWidget method is called otherwise.
 */
void SearchWindow::searchAndShow(QString text)
{
    lineEdit->setText(text);
    QString blank = "";
    QString defaultstr = "Type your word here";

    if(lineEdit->text().isEmpty())
    {
        listWidget->clear();
        update();
    }
    else//same as in return presses slot
    {
        updateListWidget(lineEdit->text());//change widget to display all possible search matches
        update();
    }
}

/* UpdateListWidget Method
 * This method is used to update the list widget to contain only items that match the search word(s).
 * After clearing any previous items from the listWidget, it uses a vector of matches to the given QString
 * to repopulate the listWidget. To do this it creates a new QListWidgetItem for each matching word
 * and sets the parent widget to be the listWidget.
 */
void SearchWindow::updateListWidget(QString text)
{
    // search for the actual synonyms.
    // this is the first time find() is used.
    // next time is to get from list widget item to Phrase pointer
    QList<Phrase*> matches = find(text); // search is case-insensitive
    foreach (Phrase* p, matches) {
        qDebug() << p->getName() << endl;
    }

    listWidget->clear();//if there is no match to the word typed, the widget will be blank
    if(matches.size()>0)//show the word they selected and synonyms
    {
        for(int i=0; i<matches.size(); i++)
        {
            QListWidgetItem *item = new QListWidgetItem(matches[i]->getName(), listWidget);
            listWidget->addItem(item); //add a new item to the listWidget
            if(i==0)
            {
                item->setSelected(true);
            }
        }
        removeDuplicates(); //remove any duplicates that may have occured
    }
}

// thesaurus wrapper
QList<Phrase*> SearchWindow::find(QString text)
{
    return thesaurus->lookup(text);
}

/* RemoveDuplicates Method
 * Searches through the listWidget and hides any duplicates that occur
 */
void SearchWindow::removeDuplicates()
{
    for(int i=0; i<listWidget->count(); i++)
    {
        for(int j=i+1; j<listWidget->count(); j++)
        {
            if(listWidget->item(i)->text().compare(listWidget->item(j)->text())==0)
            {
               listWidget->item(j)->setHidden(true);
            }
        }
    }
}

/* Typing Finished Slot
 * This slot handles what happens when the user has finished typing the word
 * It calls the methods to update the listWidget as needed
 */
void SearchWindow::typingFinished()
{
    QString blank = ""; //blank sting to be used to compare to the input --> if its blank the user must be able to see all the options
    if(blank.compare(lineEdit->text())==0)//show all
    {
        // clear when text blank
        listWidget->clear();
    }
    else //show search matches --> will be blank if there are no matches
    {
        updateListWidget(lineEdit->text());
    }
}

// set the Thesaurus object used by this window
void SearchWindow::setThesaurus(Thesaurus *t) {thesaurus = t;}

SearchWindow::~SearchWindow() {}
